#1  数据分布 
setwd("C:\\Users\\lenovo\\Desktop\\R语言")    #设置文件路径
ADdata=read.csv("merdata.csv")      #(以下三行)读取ADdata数据并将基因名置为行名
rownames(ADdata)=ADdata[,1]
ADdata=ADdata[-1]
GSEdata=read.csv("finaldata.csv")   #(以下三行)读取GSE数据并将基因名置为行名
rownames(GSEdata)=GSEdata[,1]
GSEdata=GSEdata[-1]
data2=GSEdata[4:22090,]     #去掉前三行
data1=unlist(ADdata)      #将ADdata数据变为一维形式
data3=unlist(data2)       #将GSE数据变为一维形式 
windows()
#(以下一行)为ADdata画直方图(总体分布)
hist(data1,xlab="ADdata数据",main=paste("Histogram of","ADdata"))


#(以下行)ADdata数据的处理：
data1=data1[which(data1!=0)]    #数据去零
hist(data1,xlab="ADdata数据",main=paste("Histogram of","ADdata"))


data1=log(data1)               #数据取log
hist(data1,xlab="ADdata数据",main=paste("Histogram of","ADdata"))


#(以下一行)为GSE进行数据处理和画直方图(总体分布)
data3=data3[which(data3!=0)]  #数据去零
data3=as.numeric(data3)    #数据转化为数值型
hist(data3,xlab="GSE67835数据",main=paste("Histogram of","GSE67835"))
data3=log(data3)           #数据取log
hist(data3,xlab="GSE67835数据",main=paste("Histogram of","GSE67835"))



#2 绘制火山图
setwd("C:\\Users\\lenovo\\Desktop\\R语言")  #设置路径
load("class5_volcano.RData")       #载入数据
bgdata=prostat
install.packages("ggrepel")            #(以下四行) 安装和载入包
install.packages("ggplot2")
library("ggrepel")
library("ggplot2")
pthreshold=0.05             #(以下两行)设置P和FC的阈值
fcthreshold=1.2
# 设置点的分类
bgdata$change <- as.factor(ifelse(bgdata$P<pthreshold & abs(bgdata$FC)>log2(fcthreshold),
                              ifelse(bgdata$FC>log2(fcthreshold),"Up","Down"),"Non"))

# 样本标签
bgdata$label <- ifelse(bgdata$P<pthreshold & abs(bgdata$FC)>log2(fcthreshold),bgdata$ID,"")

# 绘制火山图
p.vol <- ggplot(data = bgdata,
                aes(x = FC,y = -log10(P),colour = change,fill = change))+
  scale_color_manual(values = c('green','grey','red'))+
  geom_point(alpha = 0.4,size = 3.5)+
  # 标签
  geom_text_repel(aes(x = FC,y = -log10(P),label = label),size = 3,
                  box.padding = unit(0.6,"lines"),point.padding = unit(0.7,"lines"),
                  segment.color = "black",show.legend = FALSE)+
  # 辅助线
  geom_vline(xintercept = c(-log2(fcthreshold),log2(fcthreshold)),lty = 4,col = "black",lwd = 0.8)+
  geom_hline(yintercept = -log10(pthreshold),lty = 4,col = "black",lwd = 0.8)+
  theme_bw()+
  labs(x = "log2(FoldChange)",y = "-log10(p)",title = "Volcano Plot of  Different Expression Proteins")+
  # 坐标轴标题、标签和图例相关设置
  theme(axis.text = element_text(size = 11),axis.title = element_text(size = 13), # 坐标轴标签和标题
        plot.title = element_text(hjust = 0.5,size = 15,face = "bold"), # 标题
        legend.text = element_text(size = 11),legend.title = element_text(size = 13), # 图例标签和标题
        plot.margin = unit(c(0.5,0.5,0.5,0.5),"cm")) # 图边距
ggsave(p.vol,filename = "Volcano.pdf")



#3  树状图
distance<-dist(ADdata)     #计算距离
tree<-hclust(distance)      #用hclust函数进行聚类
plot(tree)                 #画图